package mysql.cs;

import io.netty.buffer.ByteBuf;
import mysql.MysqlPacket;
import mysql.cs.packet.QueryResField41Packet;
import mysql.cs.packet.QueryResHeaderPacket;
import mysql.cs.packet.RowDataPacket;
import mysql.cs.prop.CurrentOp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class QueryComManager extends ToStringClass {
    private QueryResHeaderPacket queryResHeaderPacket = new QueryResHeaderPacket();
    private List<QueryResField41Packet> queryResField41PacketList = new ArrayList<>();
    private List<RowDataPacket> rowDataPacketList = new ArrayList<>();
    private byte seqid = (byte) 0x00;

    public QueryComManager() {

    }

    public void encode(ByteBuf byteBuf,ComAndPackManager comAndPackManager) throws IOException {
        while (byteBuf.readableBytes() > 4) {
            byteBuf.markReaderIndex();
            int length = byteBuf.readMediumLE();
            System.out.println("当前长度：" + length);
            byte seqid = byteBuf.readByte();

            if(byteBuf.readableBytes()<length){
                byteBuf.resetReaderIndex();
                break;
            }

            byte[] data = new byte[length];
            System.out.println((long) (seqid + (byte) 0x01) <= queryResHeaderPacket.getNumberOfCol());
            System.out.println("当前seq：" + ((long) (seqid - (byte) 0x01)));
            //direct to stack

            byteBuf.readBytes(data);
            if (data[0] == (byte) 0xfe) {
                System.out.println("列或数据读取结束EOF   !!!!!!!!!");
                if(comAndPackManager.getCurrentOp()==CurrentOp.QUERYSQL){
                    comAndPackManager.setCurrentOp(CurrentOp.QUERYSQLFIELD);
                }else {
                    comAndPackManager.setCurrentOp(CurrentOp.QUERYSQLDATA);
                }
                continue;
            }
            if (seqid == (byte) 0x01) {
                queryResHeaderPacket.encode(data);
            } else if ((seqid - (byte) 0x01) <= queryResHeaderPacket.getNumberOfCol()) {
                QueryResField41Packet tmpQueryResField41Packet = new QueryResField41Packet();
                tmpQueryResField41Packet.encode(data);
                queryResField41PacketList.add(tmpQueryResField41Packet);
                System.out.println(tmpQueryResField41Packet.toString());
                System.out.println("列总数：" + queryResHeaderPacket.getNumberOfCol());

            } else if (seqid - (byte) 0x01-(byte)0x01/*列EOF*/>queryResHeaderPacket.getNumberOfCol()) {
                RowDataPacket tmpRowDataPacket = new RowDataPacket();
                tmpRowDataPacket.encode(data);
                rowDataPacketList.add(tmpRowDataPacket);
                System.out.println(tmpRowDataPacket);
            }

        }


    }

    public QueryResHeaderPacket getQueryResHeaderPacket() {
        return queryResHeaderPacket;
    }

    public List<QueryResField41Packet> getQueryResField41PacketList() {
        return queryResField41PacketList;
    }

    public List<RowDataPacket> getRowDataPacketList() {
        return rowDataPacketList;
    }
}
